package com.lanxin.jet.services.impl;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.lanxin.jet.beans.UserBean;
import com.lanxin.jet.beans.UserTokenBean;
import com.lanxin.jet.mappers.UserMapper;
import com.lanxin.jet.services.UserService;
import com.lanxin.jet.tools.JetCriteria;
import com.lanxin.jet.tools.JetDate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserTokenBean add(String account, String password, String name, String ip) {
        password = DigestUtils.md5DigestAsHex(password.trim().getBytes());
        UserBean user = new UserBean();
        user.setId(JetDate.uniqid());
        user.setAccount(account);
        user.setPassword(password);
        user.setName(name);
        user.setIp(ip);
        user.setCreateTime(JetDate.timeNow());
        user.setStatus(STATUS_NORMAL);
        userMapper.insert(user);
        return buildToken(user, ip);
    }

    @Override
    public int count(int status, String keyword) {
        JetCriteria criteria = new JetCriteria();
        if (status != -1) {
            criteria.add("status", status);
        }
        criteria.addLike("account", keyword);
        return userMapper.count(criteria);
    }

    @Override
    public UserBean get(String id) {
        return userMapper.query(id);
    }

    @Override
    public UserBean getAccount(String account) {
        return userMapper.queryByAccount(account);
    }

    @Override
    public List<UserBean> getList(int offset, int limit, int status, String keyword) {
        JetCriteria criteria = new JetCriteria();
        criteria.setSelect("id,account,name,avatar,gender,role,count,ip,createTime,activeTime,status");
        criteria.setPage(offset, limit);
        if (status != -1) {
            criteria.add("status", status);
        }
        criteria.addLike("account", keyword);
        criteria.orderBy("createTime", JetCriteria.ORDER_DESC);
        return userMapper.queryAll(criteria);
    }

    @Override
    public void update(UserBean user) {
        userMapper.update(user);
    }

    @Override
    public void delete(String id) {
        userMapper.delete(id);
    }

    @Override
    public boolean existAccount(String account) {
        if (account.isEmpty()) {
            return false;
        }
        JetCriteria criteria = new JetCriteria();
        criteria.add("account", account);
        if (userMapper.count(criteria) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public UserTokenBean buildToken(UserBean user, String ip) {
        String token = DigestUtils.md5DigestAsHex(JetDate.uniqid().getBytes());
        long timestamp = JetDate.timeNow();
        user.setToken(token);
        user.setIp(ip);
        user.setCount(user.getCount() + 1);
        user.setActiveTime(timestamp);
        userMapper.update(user);
        return new UserTokenBean(user.getId(), token, timestamp);
    }

    @Override
    public boolean checkToken(UserTokenBean userToken) {
        if (!userToken.getToken().isEmpty()) {
            UserBean user = userMapper.query(userToken.getUid());
            if (user != null) {
                if (user.getStatus() > STATUS_LOCK && user.getToken().equals(userToken.getToken())) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean checkRole(HttpSession session, int role) {
        if (session.getAttribute("uid") != null) {
            UserBean user = userMapper.query(session.getAttribute("uid").toString());
            if (user != null) {
                int roles = user.getRole();
                if ((roles & role) > 0) {
                    return true;
                }
            }
        }
        return false;
    }

}